package yanyan.com.tochar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.gs.buluo.common.widget.LoadingDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import yanyan.com.tochar.adpter.FullyGridLayoutManager;
import yanyan.com.tochar.adpter.GridImageAdapter;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.PictureSelectUtil;
import yanyan.com.tochar.utils.ToCharUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * 像素图
 */
public class FunsPixelImgActivity extends Activity {
    private RecyclerView recyclerView;
    private GridImageAdapter adapter;
    private ImageView imgRes;
    private SeekBar propSeekBar;
    private List imgList= new ArrayList<>();//图片集合
    private Bitmap newBitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_pixel);
        recyclerView=findViewById(R.id.pixel_recycler);
        imgRes = findViewById(R.id.pixel_res_img);
        propSeekBar=findViewById(R.id.pixel_prop_seekBar);

        //初始化图片选择
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new GridImageAdapter(this, new GridImageAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                PictureSelectUtil.selectPohto(FunsPixelImgActivity.this,3306);
            }
        });
        adapter.setList(imgList);
        adapter.setSelectMax(1);
        recyclerView.setAdapter(adapter);

        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_pixel_img");
    }


    /**
     * 选择图片事件
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            imgList = PictureSelector.obtainMultipleResult(data);
            adapter.setList(imgList);
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 按钮点击事件
     * @param view
     */
    public void createPixelImg(View view){
        if(imgList!=null && imgList.size()>0){
            LoadingDialog.getInstance().show(FunsPixelImgActivity.this, "正在生成", false);//加载
            Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(ObservableEmitter<String> e) throws Exception {
                    try{
                        int prop=propSeekBar.getProgress();
                        LocalMedia localMedia= (LocalMedia) imgList.get(0);
                        String path="";
                        if(CoreUtil.isAndroidQ()){
                            path=localMedia.getAndroidQToPath();
                        }else {
                            path= localMedia.getPath();
                        }
                        Bitmap bitmap = PictureSelectUtil.getBitmap(path);
                        int width = bitmap.getWidth();
                        int p=width/2000;
                        if(p>1){
                            bitmap=PictureSelectUtil.getCompressPhoto(path,p);
                        }
                        newBitmap= getPicelImg( prop,bitmap);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                imgRes.setImageBitmap(newBitmap);
                            }
                        });
                        e.onComplete();
                    }catch (Exception e1){
                        e.onError(e1);
                    }
                }
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
                @Override
                public void onSubscribe(Disposable d) {
                }
                @Override
                public void onNext(String s) {

                }
                @Override
                public void onError(Throwable e) {
                    LoadingDialog.getInstance().dismissDialog();
                    ToastUtil.showShotToast(FunsPixelImgActivity.this, "生成失败");
                }
                @Override
                public void onComplete() {
                    LoadingDialog.getInstance().dismissDialog();
                    ToastUtil.showShotToast(FunsPixelImgActivity.this, "生成成功");
                }
            });
        }else {
            ToastUtil.showLongToast(this,"请选择图片");
        }

    }

    /**
     * 图片保存
     * @param view
     */
    public void savenPixelImg(View view) {
        if(newBitmap!=null){
            String s = PictureSelectUtil.savePhotoToSD(newBitmap, this);
            ToastUtil.showShotToast(this,"图片已保存至"+s);
        }else {
            ToastUtil.showShotToast(this,"您还没有生成");
        }
    }



    private Bitmap getPicelImg(int prop,Bitmap bitmap){
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap resBitmap=Bitmap.createBitmap(width+prop,height+prop,Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(resBitmap);
        //线的笔
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(1);
        //画颜色的笔
        Paint colorPaint=new Paint();
        colorPaint.setStyle(Paint.Style.FILL);
        canvas.drawColor(Color.WHITE);
        for (int x=0;x<width;x+=prop){
            for (int y=0;y<height;y+=prop){
                int pixel = bitmap.getPixel(x, y);
                colorPaint.setColor(pixel);
                canvas.drawLine(x,y,x,y+prop,paint);
                canvas.drawLine(x,y,x+prop,y,paint);
                canvas.drawRect(x+1,y+1,x+prop-1,y+prop-1,colorPaint);
            }
        }

        return resBitmap;
    }

/**
 * 该效果不错，故留下
 */
//    private Bitmap gegggg(int prop,Bitmap bitmap){
//        int width = bitmap.getWidth();
//        int height = bitmap.getHeight();
//
//        Bitmap resBitmap=Bitmap.createBitmap(width+prop,height+prop,Bitmap.Config.ARGB_8888);
//        Canvas canvas=new Canvas(resBitmap);
//        Paint paint = new Paint();
//        paint.setColor(Color.BLACK);
//        paint.setStrokeWidth(1);
//        canvas.drawColor(Color.WHITE);
//        for (int x=0;x<width;x+=prop){
//            for (int y=0;y<height;y+=prop){
//                canvas.drawLine(x,y,x,y+prop,paint);
//                canvas.drawLine(x,y,y,x+prop,paint);
//            }
//        }
//
//        return resBitmap;
//    }

}
